#!/usr/bin/env python
#coding=utf-8

import wx
class CategoryView(wx.Frame):
    ''' Frame for entering and updating categories.

        Methods:
        --------
        display_dialog_box()       - Show dialog box for category input
        getSelectedCategory()      - Returns idx
        getSelectedCategoryValue() - returns category as string
        setCancelEnabled()         - Enable/Disable cancel button
        set_order_label()          - Set label of the sort button
        setSaveEnabled()           - Enable/Disable save button
        setWindowTitle()           - writes category into window title bar
    '''

    def __init__(self):
        ''' '''

        wx.Frame.__init__(self, None,size = (500,650))
        self.SetBackgroundColour("lightgray")
        self.categoryLst = wx.ListBox(self,
                    size = (200,600))
        self.categoryInput = wx.TextCtrl(self, size=(160, -1))
        self.cancelBtn = wx.Button(self,label='Cancel')
        self.submitBtn = wx.Button(self,label='Save')
        self.addBtn = wx.Button(self, label="New Category")
        self.orderBtn = wx.Button(self, label="A->Z")

        categoryTxt = wx.StaticText(self, label='Category')

        mainSizer = wx.BoxSizer(orient=wx.HORIZONTAL)
        leftBox = wx.GridBagSizer(5,5)
        rightBox = wx.BoxSizer(orient=wx.VERTICAL)
        inputBox = wx.FlexGridSizer(rows=1,cols=2,vgap=2,hgap=2)
        actionBox = wx.BoxSizer(orient=wx.HORIZONTAL)

        # Adding items to the sizers
        #leftBox.Add(self.categoryLst)
        leftBox.Add(self.categoryLst,(0,0), (1,2),flag=wx.EXPAND)
        leftBox.Add(self.addBtn, (1,0), (1,1), flag=wx.EXPAND)
        leftBox.Add(self.orderBtn, (1,1), (1,1), flag=wx.EXPAND)
        inputBox.Add(categoryTxt,flag=wx.TOP,border=20)
        inputBox.Add(self.categoryInput,flag=wx.TOP,border=20)

        actionBox.Add(self.cancelBtn)
        actionBox.Add(self.submitBtn)

        # Arrange sizers
        rightBox.Add(inputBox,flag=wx.LEFT,border=20)
        rightBox.Add(actionBox,flag=wx.LEFT|wx.TOP,border=20)

        mainSizer.Add(leftBox, flag=wx.LEFT|wx.TOP,border=10)
        mainSizer.Add(rightBox, flag=wx.LEFT|wx.TOP,border=10)

        self.SetSizer(mainSizer)

    def display_dialog_box(self):
        ''' Show dialog box for category input. '''
        
        dlg = wx.TextEntryDialog(self, 'Enter category','New Category')
        dlg.SetValue("Default")
        if dlg.ShowModal() == wx.ID_OK:
            category = str(dlg.GetValue())
        dlg.Destroy()     
        return category   

    def getCategory(self):
        ''' Return category from category input field. '''
        return self.categoryInput.GetValue()

    def getSelectedCategory(self):
        return self.categoryLst.GetSelection()

    def getSelectedCategoryValue(self):
        return self.categoryLst.GetString(self.categoryLst.GetSelection())

    def setCancelEnabled(self, enabled):
        ''' Change state of cancel button. '''

        self.cancelBtn.Enable(enabled)

    def setCategories(self, categories):
        '''
        This method contains a small optimisation to take care of the flicker
        that appears during the updates. Exercise: comment out the Freeze
        and Thaw lines and see what happens
        '''
        self.Freeze()
        self.categoryLst.Set([category.category for category in categories])
        self.Thaw()

    def setCategory(self, category):
        self.categoryInput.SetValue(category)

    def set_order_label(self, label):
        ''' Change the label of the sort button. '''

        self.orderBtn.SetLabel(label)
    
    def setSaveEnabled(self, enabled):
        ''' Change state of submit button. '''

        self.submitBtn.Enable(enabled)

    def setSelectedCategory(self, idx):
        self.categoryLst.SetSelection(int(idx),True)

    def setWindowTitle(self, title):
        ''' Writes category into window title bar.

            title -> str()
         '''

        self.SetTitle(title)

    def start(self):
        ''' Upon start we just show the frame. '''

        self.CenterOnScreen()
        self.Show()